﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Forms;
using Cow;

namespace CowUI
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = "选择文件";
            fileDialog.Filter = "xml files (*.xlsx)|*.xlsx";
            fileDialog.FilterIndex = 1;
            fileDialog.RestoreDirectory = true;
            fileDialog.Multiselect = true;
            fileDialog.ShowDialog();
            if (fileDialog.FileNames.Any())
            {
                dataGrid1.CanUserDeleteRows = true;
                dataGrid1.ItemsSource = fileDialog.FileNames.Select(item => new { FileName = item }).ToList();
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            folderBrowserDialog1.Description = "输出目录";
            folderBrowserDialog1.ShowNewFolderButton = true;
            //folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal;
            System.Windows.Forms.DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                var targetList = GetTargetList();
                string folderName = folderBrowserDialog1.SelectedPath;
                /*
                //实例一个Process类，启动一个独立进程  
                Process p = new Process();
                //Process类有一个StartInfo属性，这个是ProcessStartInfo类，包括了一些属性和方法，下面我们用到了他的几个属性：
                p.StartInfo.FileName = "Cow.exe";           //设定程序名
                p.StartInfo.UseShellExecute = false;        //关闭Shell的使用
                p.StartInfo.RedirectStandardInput = true;   //重定向标准输入
                p.StartInfo.RedirectStandardOutput = true;  //重定向标准输出
                p.StartInfo.RedirectStandardError = true;   //重定向错误输出
                p.StartInfo.CreateNoWindow = true;          //设置不显示窗口
                dynamic fileNameList = dataGrid1.ItemsSource;
                foreach (dynamic item in fileNameList)
                {
                    var inputFile = item.FileName;
                    foreach (var target in targetList) 
                    {
                        p.StartInfo.Arguments = String.Format("-i\"{0}\" -o\"{1}\" -t{2}", inputFile, folderBrowserDialog1.SelectedPath, target);
                        if (!String.IsNullOrWhiteSpace(textBox1.Text)) 
                        {
                            p.StartInfo.Arguments += "-n" + textBox1.Text.Trim();
                        }
                        p.Start();   //启动
                        p.StandardInput.WriteLine("exit"); 
                    }
                }*/

                var codeFactory = new CodeFactory();
                dynamic fileNameList = dataGrid1.ItemsSource;
                var errorMessage = new StringBuilder();
                foreach (dynamic item in fileNameList)
                {
                    var inputFile = item.FileName;
                    foreach (var target in targetList)
                    {
                        var codeTarget = (CodeTarget)Enum.Parse(typeof(CodeTarget), target);
                        String message = codeFactory.CreateCode(inputFile, textBox1.Text.Trim(), folderBrowserDialog1.SelectedPath, codeTarget);
                        if (!String.IsNullOrWhiteSpace(message))
                        {
                            errorMessage.AppendLine(message);
                        }
                    }
                }
                if (errorMessage.Length != 0)
                {
                    System.Windows.Forms.MessageBox.Show(errorMessage.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("操作成功！");
                }
            }
        }

        private List<String> GetTargetList()
        {
            List<Target> targetList = new List<Target>();
            targetList.Add(GetTarget(chkCreate));
            targetList.Add(GetTarget(chkEdit));
            targetList.Add(GetTarget(chkEntity));
            targetList.Add(GetTarget(chkMap));
            targetList.Add(GetTarget(chkList));
            targetList.Add(GetTarget(chkService));
            targetList.Add(GetTarget(chkSchema));
            targetList.Add(GetTarget(chkView));
            targetList.Add(GetTarget(chkViewModel));
            return targetList.Where(item => item.Active).Select(item => item.TargetName).ToList();
        }

        private Target GetTarget(System.Windows.Controls.CheckBox checkBox)
        {
            return new Target() { Active = checkBox.IsChecked.Value, TargetName = checkBox.Content.ToString() };
        }
    }

    public class Target
    {
        public Boolean Active { get; set; }
        public String TargetName { get; set; }
    }
}
